package com.shihan.redis.springbootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author shihan
 * @Date 2020/11/29 10:09
 * @Version 1.0
 */
@RestController     // @RestController = @Controller + @ResponseBody
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * TODO @RequestParam、@RequestBody
     * @param key
     * @param value
     * @return
     */
    @GetMapping("/put")
    public String put(@RequestParam(name = "key",required = true) String key,
                      @RequestParam(name = "value",required = true) String value){
        //为什么 set 没有返回值，这样不是没法知道是否 set 成功吗？
        redisTemplate.opsForValue().set(key,value,20, TimeUnit.SECONDS);
        return "success";
    }

    @GetMapping("/get")
    public String get(@RequestParam(name = "key",required = true) String key){
        String str = (String)redisTemplate.opsForValue().get(key);
        return str;
    }

}
